<%@ include file="/includes/taglibs.jsp"%>
<ul class="breadcrumb">
	<li><a href="<c:url value="/users/list.html"/>">Menu Access</a> <span
		class="divider">/</span></li>
	<li class="active">Add or Update data</li>
</ul>

Menu access for user <b>${user.username}</b>
<br/><br/>
<div id="menuTree"></div>
<br/>
<input type="button" id="saveBtn" class="btn btn-primary" value="Save"/>
<button type="button" class="btn"
			onclick="location.href='<c:url value="/menuaccess/list.html"/>'">Cancel</button>

<script type="text/javascript">
	$(document).ready(function(){
		$('#menuTree').jstree({ 
			"json_data" : {
				"ajax" : {
					"url" : "<c:url value="/menuaccess/getmenu.json"/>",
					"data" : function (n) { 						
						return { 
							id : n.attr ? n.attr("id") : 0,
							precheck: ${granted}
						}; 
					}
				}
			},
			"themes" : {
				"theme" : "default",
				"dots" : true,
				"icons" : true
			},
			"plugins" : [ "themes", "json_data", "checkbox", "sort", "ui" ]
		});
		
		$('#saveBtn').click(function(){
			var checked_ids = [];
			$('#menuTree').jstree("get_checked",null,true).each 
            (function () { 
                checked_ids.push(this.id); 
            }); 
			console.dir(checked_ids);
			
			//console.log(JSON.stringify(json));
			$.ajax({
				type : "POST",
				url : "<c:url value="/menuaccess/save" />",
				data : "userid=${user.userid}&menuid="+checked_ids,
				error: function(e) {
				    console.log(e.responseText);
				}
			}).done(function(msg) {
				alert("Data Saved: " + msg);
				window.location.href = "<c:url value="/menuaccess/list.html"/>";
			});
		});
	});
</script>